package com.h3c.nem.config.mq.consumer;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.h3c.nem.config.properties.EmailSendProperties;
import com.h3c.nem.constant.NemManageConstant;
import com.h3c.nem.entity.TbOfferWorker;
import com.h3c.nem.service.ITbOfferWorkerService;
import com.h3c.nem.utils.EmailUtil;
import com.h3c.nem.utils.RedisUtil;
import com.rabbitmq.client.Channel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author chenyifan
 * @date 2022/7/28 10:52
 */
@Slf4j
@AllArgsConstructor
@Component
public class EmailAckReceiver implements ChannelAwareMessageListener {

    private final ITbOfferWorkerService offerWorkerService;

    private final EmailSendProperties emailSendProperties;

    public static final String EMAIL_MQ = NemManageConstant.PREFIX_COMMON_CACHE + "email:{}";

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {

            JSONObject parseObj = JSONUtil.parseObj(message.getBody());
            String workerId = parseObj.getStr("workerId");
            if (!RedisUtil.setIfAbsent(StrUtil.format(EMAIL_MQ, workerId), "exist", 5, TimeUnit.MINUTES)) {
                channel.basicAck(deliveryTag, false);
                return;
            }
            String signUrl = parseObj.getStr("signUrl");
            String templateCode = parseObj.getStr("templateCode");
            HashMap<String, String> params = new HashMap<>(2);
            TbOfferWorker offerWorker = offerWorkerService.getById(workerId);
            if(Objects.isNull(offerWorker)){
                channel.basicAck(deliveryTag, false);
                return;
            }
            params.put("workerNameAndId",workerId.concat("/").concat(offerWorker.getWorkerName()));
            params.put("signUrl",signUrl);
            if(offerWorker.getWorkCity().contains("杭州")){
                EmailUtil.sendPtEmail(templateCode,params,emailSendProperties.getHangzhouEmailReceiver(),"员工签署完毕，请即时审核文件无误后签署公司章",null,null);
            }else if(offerWorker.getWorkCity().contains("北京")){
                EmailUtil.sendPtEmail(templateCode,params,emailSendProperties.getBeijingEmailReceiver(),"员工签署完毕，请即时审核文件无误后签署公司章",null,null);
            }else if(offerWorker.getWorkCity().contains("成都")){
                EmailUtil.sendPtEmail(templateCode,params,emailSendProperties.getChengduEmailReceiver(),"员工签署完毕，请即时审核文件无误后签署公司章",null,null);
            }else{
                EmailUtil.sendPtEmail(templateCode,params,emailSendProperties.getOtherEmailReceiver(),"员工签署完毕，请即时审核文件无误后签署公司章",null,null);
            }
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            channel.basicReject(deliveryTag, false);
            //考虑记录消费不重发消息
            log.error("消息消费失败,消息内容:" + message.getBody() + ",失败原因:{}", e);
        }
    }
}
